//
//  Place_data.swift
//  Chufang
//
//  Created by zhu xietong on 2017/9/19.
//  Copyright © 2017年 zhu xietong. All rights reserved.
//

import Foundation
import Eelay


typealias _keys = Place.Tables
public enum PlaceNode{
    case province
    case city
    case area
    public var valueKey:String{
        let keys = self.keys
        return keys.value
    }
    
    public var keys:(table:String,parent:String,id:String,code:String,name:String,value:String){
        get{
            switch self {
            case .province:
                return (_keys.province.table,"",_keys.province.id,_keys.province.code,_keys.province.name,_keys.province.value)
            case .city:
                return (_keys.city.table,_keys.city.parent,_keys.city.id,_keys.city.code,_keys.city.name,_keys.city.value)
            case .area:
                return (_keys.area.table,_keys.area.parent,_keys.area.id,_keys.area.code,_keys.area.name,_keys.area.value)
            }
        }

    }
    
    public func get(_ id:String?=nil) ->[String:Any]?{
        let keys = self.keys
        let sql = "select * from \(keys.table) where \(keys.value) = '\(id ?? "XXX")'"
        let list = Place.db.query(sql: sql)
    
        if list.count > 0
        {
            return list.first
        }
        return nil
    }
    

    
    public func filter(_ parent:String?=nil) -> [[String:Any]] {
        let keys = self.keys
        var _parent = parent

        switch self {
        case .province:
            _parent = nil
        default:
            break
        }
        
        if let p = _parent{
            let sql = "select * from \(keys.table) where \(keys.parent) = '\(p)'"
            let list = Place.db.query(sql: sql)
            return list
        }else{
            let list = Place.db.query(sql: "select * from \(keys.table)")
            return list
        }
    }
}



extension Place{
    
    // value值为选择器提交时选用哪个字段作为值使用
    //以下配置主要方便更换地区数据库
    public struct Tables {
        public struct province {
            public static var table:String = "province"
            public static var name:String = "name"
            public static var code:String = "code"
            public static var id:String = "id"
            public static var value:String = "province_id"
        }
        
        public struct city {
            public static var table:String = "city"
            public static var parent:String = "province_id"
            public static var name:String = "name"
            public static var code:String = "code"
            public static var id:String = "id"
            public static var value:String = "city_id"
            
        }
        
        public struct area {
            public static var table:String = "area"
            public static var parent:String = "city_id"
            public static var name:String = "name"
            public static var code:String = "code"
            public static var id:String = "id"
            public static var value:String = "area_id"
        }
    }
}


public class Place
{
    public static var db:DB.sqlite = DB.sqlite(db_name: "china_regions.db", bundle: Bundle(for: Place.self))    
    public static func addressIDs(with name:String) ->(ids:String,json_value:String)
    {

        let names = name.components(separatedBy: "|")
        if names.count > 2
        {
            let province_name = names[0]
            let city_name = names[1]
            let area_name = names[2]
            var json_value:[String:Any] = [String:Any]()
            
            let p_keys = PlaceNode.province.keys
            let c_keys = PlaceNode.city.keys
            let a_keys = PlaceNode.area.keys

            let sql0 = "select * from \(p_keys.table) where \(p_keys.name) = '\(province_name)'"
            let list0 = Place.db.query(sql: sql0)
            if list0.count > 0
            {
                let province_id = list0.first!.mutable_dictionary[p_keys.value,""]
                let sql1 = "select * from \(c_keys.table) where \(c_keys.name) = '\(city_name)' and \(c_keys.parent) = \(province_id)"
                let list1 = Place.db.query(sql: sql1)
                
                json_value["province"] = list0.first!

                
                if list1.count > 0{
                    let city_id = list1.first!.mutable_dictionary[c_keys.value,""]
                    json_value["city"] = list1.first!


                    let sql2 = "select * from \(a_keys.table) where \(c_keys.name) = '\(area_name)' and \(a_keys.parent) = \(city_id)"
                    let list2 = Place.db.query(sql: sql2)
                    if list2.count > 0
                    {
                        let area_id = list2.first!.mutable_dictionary[a_keys.value,""]
                        json_value["area"] = list2.first!


                        let vs = [province_id,city_id,area_id]
                        return (vs.joined(separator: "|"),(json_value.rawJson ?? "{}"))
                    }
                }
            }
        }
        
        return ("","")
        
    }
    
//    public static func province(ID:String?) ->[String:Any]?{
//        let sql = "select * from province where \(PlaceLever.province.idKey) = '\(ID ?? "XXX")'"
//        let list = Place.db.query(sql: sql)
//        if list.count > 0
//        {
//            return list.first
//        }
//        return nil
//    }
//    public static func city(ID:String?) ->[String:Any]?{
//        let sql = "select * from city where \(PlaceLever.city.idKey) = '\(ID ?? "XXX")'"
//        let list = Place.db.query(sql: sql)
//        if list.count > 0
//        {
//            return list.first
//        }
//        return nil
//    }
//    
//    public static func area(ID:String?) ->[String:Any]?{
//        let sql = "select * from area where \(PlaceLever.area.idKey) = '\(ID ?? "XXX")'"
//        let list = Place.db.query(sql: sql)
//        if list.count > 0
//        {
//            return list.first
//        }
//        return nil
//    }
    
//    static func place(ID:String?) ->[String:Any]?{
//        let sql = "select * from place where id = '\(ID ?? "XXX")'"
//        let list = Place.db.query(sql: sql)
//        if list.count > 0
//        {
//            return list.first
//        }
//        return nil
//    }
    
    
    
    
//    public static var province_list:[[String:Any]]{
//        get{
//            let list = Place.db.query(sql: "select * from province")
//            return list
//        }
//    }
//    public static func city_list(provinceID:String="530000") ->[[String:Any]]{
//        let sql = "select * from city where \(PlaceLever.province.idKey) = '\(provinceID)' order by order_index desc"
//        let list = Place.db.query(sql: sql)
//        return list
//    }
//
//    public static func area_list(cityID:String) ->[[String:Any]]{
//        let sql = "select * from area where \(PlaceLever.city.idKey) = '\(cityID)'"
//        let list = Place.db.query(sql: sql)
//        return list
//    }
    
//    static func place_list(areaID:String) ->[[String:Any]]{
//        let sql = "select * from place where pid = '\(areaID)'"
//        print("00000|\(sql)")
//
//        let list = Place.db.query(sql: sql)
//        return list
//    }
    
}
